package com.lin.views;

import java.util.ArrayList;

import com.lin.utils.Parameters;
import com.lin.utils.Utils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.manager.SuperFriendsManager;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewForSuperFriends extends ListView {

	private int TYPE_ITEM = 0;
	private int TYPE_FRIENDS = 1;
	private int TYPE_COUNT = 2;

	MyAdapter mFocusGameAdapter;
	Context mContext;
	DisplayImageOptions options;

	public ListViewForSuperFriends(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

		mContext = context;
		options = new DisplayImageOptions.Builder()
				.displayer(
						new RoundedBitmapDisplayer(
								context.getResources().getDimensionPixelOffset(
										R.dimen.corners_avatar)))
				.showImageOnFail(R.drawable.default_avatar)
				.showImageOnLoading(R.drawable.default_avatar)
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

	}

	public void setDataList(ArrayList<SuperFriendsBean> mFocusGameList) {
		if (mFocusGameAdapter == null) {
			mFocusGameAdapter = new MyAdapter(getContext(), mFocusGameList);
			setAdapter(mFocusGameAdapter);
		} else {
			mFocusGameAdapter.setmFocusGameList(mFocusGameList);
			mFocusGameAdapter.notifyDataSetChanged();
		}
	}

	private class MyAdapter extends BaseAdapter {

		LayoutInflater mLayoutInflater;
		ArrayList<SuperFriendsBean> mDataList;
		int mSize;

		public MyAdapter(Context context, ArrayList<SuperFriendsBean> mDataList) {
			// TODO Auto-generated constructor stub
			this.mDataList = mDataList;
			mLayoutInflater = LayoutInflater.from(mContext);
		}

		public ArrayList<SuperFriendsBean> getDataList() {
			return mDataList;
		}

		public void setmFocusGameList(ArrayList<SuperFriendsBean> mFocusGameList) {
			this.mDataList = mFocusGameList;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			try {
				//mSize = mDataList.size() + 4;
				mSize = mDataList.size() + 5;
			} catch (NullPointerException e) {
				mSize = 0;
			}
			return mSize;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		public int getViewTypeCount() {
			// TODO Auto-generated method stub
			return 3;
		}

		@Override
		public int getItemViewType(int position) {
			// TODO Auto-generated method stub
			if (position == 0 || position == 1
					|| position == 2 || position == 3
					|| position == 4) {
				return TYPE_ITEM;
			} else if (position != getCount() - 1) {
				return TYPE_FRIENDS;
			} else {
				return TYPE_COUNT;
			}
		}

		@Override
		public SuperFriendsBean getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder viewholder = null;

			int type = getItemViewType(position);
			if (convertView == null) {
				viewholder = new ViewHolder();
				if (type == TYPE_ITEM) {
					convertView = mLayoutInflater.inflate(
							R.layout.list_item_super_friends, null);
				} else if (type == TYPE_FRIENDS) {
					convertView = mLayoutInflater.inflate(
							R.layout.list_item_super_friends, null);
				} else if (type == TYPE_COUNT) {
					convertView = mLayoutInflater.inflate(
							R.layout.list_item_super_friends_count, null);
				}
				
				if (type != TYPE_COUNT) {
					viewholder.iv = (ImageView) convertView.findViewById(R.id.iv);
					viewholder.tvLevel = (TextView) convertView
						.findViewById(R.id.level);
					viewholder.red_dot = convertView.findViewById(R.id.red_dot);
				}
				viewholder.tv = (TextView) convertView.findViewById(R.id.tv);

				convertView.setTag(viewholder);

			} else {
				viewholder = (ViewHolder) convertView.getTag();
			}

			setDivider(null);
			if (position < 4) {
				if (position == 0) {
					viewholder.tv.setText("新的超友");
					ImageLoader.getInstance().displayImage(
							"drawable://" + R.drawable.ic_new_good_friend,
							viewholder.iv, options);
				} else if (position == 1) {
					viewholder.tv.setText("官方超号");
					ImageLoader.getInstance().displayImage(
							"drawable://" + R.drawable.ic_entreprise_supercall,
							viewholder.iv, options);
				} else if (position == 2) {
					viewholder.tv.setText("我的关注");
					ImageLoader.getInstance().displayImage(
							"drawable://" + R.drawable.ic_my_focus,
							viewholder.iv, options);
				} else if (position == 3) {
					viewholder.tv.setText("我的粉丝");
					ImageLoader.getInstance().displayImage(
							"drawable://" + R.drawable.ic_my_fensi,
							viewholder.iv, options);
				}
			} else if (position != getCount() - 1) {
				SuperFriendsBean base = getItem(position - 4);

				viewholder.tv.setText(base.getNick_name());
				viewholder.tvLevel.setText(getLevel(base.getLevel()) + "");
				setLevelColor(base.getSucolor(), viewholder.tv,
						viewholder.tvLevel);
				ImageLoader.getInstance().displayImage(
						Parameters.SERVER_PIC_URL + base.getPhoto(),
						viewholder.iv, options);
				// iv.setImageURI(Utils.getURI(mContext,
				// R.drawable.ic_new_super_friend));
				// String url = Utils.getURI(mContext,
				// R.drawable.ic_new_super_friend).toString();
			} else {
				viewholder.tv.setText("共有 " + mDataList.size() + " 名超友");
			}

			if (position == 0) {
				boolean has = SuperFriendsManager.hasNewFriend(getContext());
				viewholder.red_dot.setVisibility(has ? View.VISIBLE
						: View.INVISIBLE);
			} else if (type != TYPE_COUNT) {
				viewholder.red_dot.setVisibility(View.INVISIBLE);
			}
			return convertView;

			// SuperFriendsBean.setupSingleLineContactsView(convertView, base);
		}

		String getLevel(int level) {
			String levelS = null;
			switch (level) {
			case 1:
				levelS = "新诞生";
				break;
			case 2:
				levelS = "吃奶尿床";
				break;
			case 3:
				levelS = "断奶吃饭";
				break;
			case 4:
				levelS = "说学走";
				break;
			case 5:
				levelS = "幼儿园";
				break;
			case 6:
				levelS = "小学生";
				break;
			case 7:
				levelS = "初中生";
				break;
			case 8:
				levelS = "高中生";
				break;
			case 9:
				levelS = "大学生";
				break;
			case 10:
				levelS = "硕士";
				break;
			default:
				levelS = "博士";
				break;
			}
			return levelS;
		}

		void setLevelColor(int level, TextView tv, TextView tvLevel) {
			try {

				if (level == 0) {
					tvLevel.setTextColor(getResources().getColor(
							R.color.textview_title_listview));
					tv.setTextColor(getResources().getColor(
							R.color.textview_title_listview));
				} else if (level == 1) {
					tvLevel.setTextColor(getResources().getColor(
							R.color.level_or));
					tv.setTextColor(getResources().getColor(R.color.level_or));
				} else {
					tvLevel.setTextColor(getResources().getColor(
							R.color.level_red));
					tv.setTextColor(getResources().getColor(R.color.level_red));
				}
			} catch (NullPointerException e) {

			}
		}
	}

	static class ViewHolder {
		ImageView iv;
		View red_dot;
		TextView tv;
		TextView tvLevel;
	}
}